package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.utils.DaoFactory;

import java.util.List;

public class UserServiceImpl implements UserService {

    public PageInfo<User> findAll(int pageNum, int pageSize) {
        // 获取dao代理对象
        UserDao dao = (UserDao) DaoFactory.getDaoInstance(UserDao.class);

        // 1. 设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        List<User> users = dao.findAll();
        //2. 设置分页参数对象
        PageInfo<User> pageInfo = new PageInfo<User>(users);

        return pageInfo;

    }

    @Override
    public User login(User user) {
        UserDao dao = (UserDao) DaoFactory.getDaoInstance(UserDao.class);

        return dao.login(user);
    }

    @Override
    public void save(User user) {
        UserDao dao = (UserDao) DaoFactory.getDaoInstance(UserDao.class);
        dao.save(user);
    }

    @Override
    public void deleteById(String id) {
        UserDao dao = (UserDao) DaoFactory.getDaoInstance(UserDao.class);
        dao.deleteById(id);
    }

    @Override
    public User findById(String id) {
        UserDao dao = (UserDao) DaoFactory.getDaoInstance(UserDao.class);
        return dao.findById(id);
    }

    @Override
    public void update(User user) {
        UserDao dao = (UserDao) DaoFactory.getDaoInstance(UserDao.class);
        dao.update(user);
    }

  /*  @Override
    public void checkName(String name) {

    }*/

    @Override
    public void deleteSel(String[] ids) {
        if (ids != null && ids.length > 0) {
            // 删除
            for (String id : ids) {
                UserDao dao = DaoFactory.getDaoInstance(UserDao.class);
                dao.deleteById(id);
            }
        }
    }

}
